package com.cloud.conmmand;


import com.cloud.domain.TOrder;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import org.springframework.web.client.RestTemplate;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/7/1 22:32
 * @Desc
 * @Remarks
 */
public class OrderHystrixCommandService extends HystrixCommand<TOrder> {

    RestTemplate restTemplate;

    Long id;

    public OrderHystrixCommandService(RestTemplate restTemplate, Long id) {
        super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("CLOUD-PRODUCER"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withCircuitBreakerEnabled(true).
                        withCircuitBreakerErrorThresholdPercentage(50).
                        withCircuitBreakerRequestVolumeThreshold(5).
                        withCircuitBreakerSleepWindowInMilliseconds(5000)));
        this.id = id;
        this.restTemplate = restTemplate;
    }

    @Override
    protected TOrder run() throws Exception {
        return restTemplate.getForObject("http://CLOUD-PRODUCER/domain/t-order/getById?id=" + id,
                TOrder.class);
    }

    @Override
    protected TOrder getFallback() {
        System.out.println("进入降级方法！！！");
        return null;
    }
}
